﻿@page "/datagrid/custom-validation"

@using Syncfusion.Blazor.Grids;
@using System.ComponentModel.DataAnnotations;
@using System.Text.RegularExpressions;

<SampleDescription>
    <p> This sample demonstrates the custom validation in the DataGrid using  <a target='_blank' href='https://blazor.syncfusion.com/documentation/datagrid/data-annotation/'>data annotation.</a> </p>
</SampleDescription>

<ActionDescription>
    <p>
        <b> Custom Validation</b> is used to validate the fields according to the user's condition using 
        <a target='_blank' href='https://blazor.syncfusion.com/documentation/datagrid/data-annotation/'>data annotation.</a><b> System.ComponentModel.DataAnnotations</b>
        namespace must be imported to use the Custom Validation. The user should create a new class for the field to be customized which must
        extend the <b> ValidationAttribute</b>. The <b> IsValid</b> method should be overridden to validate the end-user input.
    </p>
    <p>
        In this demo, the following changes are made in the Grid
        <ul>
            <li> The namespace <b> System.ComponentModel.DataAnnotations</b> is imported to use the Custom Validation.</li>
            <li> Created a class CustomValidationFreight for the Freight property to be validated which extends <b> ValidationAttribute</b>.</li>
            <li> Created a class CustomValidationEmployeeID for the EmployeeID property to be validated which extends <b> ValidationAttribute</b>.</li>
            <li> <b> IsValid</b> method is overidden and accessed the input entered by the end-user.</li>
            <li> Validated the input entered by the end-user.</li>
            <li> An error message is shown if the validation is wrong.</li>
            <li> Created a class CustomValidationFreight for the Freight property to be validated which extends<b> ValidationAttribute </b>that validates whether the entered Freight value is in between 1 and 1000.</li>
            <li> Created a class CustomValidationEmployeeID for the EmployeeID property to be validated which extends <b> ValidationAttribute</b> that validates whether the entered EmployeeID value is greater than 0.</li>            
        </ul>
    </p>
    <p>In this grid, try to add new record with empty values to see the validations done in columns.</p>
    <p> More information on the custom validation can be found in this <a target='_blank' href='https://blazor.syncfusion.com/documentation/datagrid/editing/#column-validation'>documentation</a> section.</p>
</ActionDescription>

<div class="col-lg-12 control-section">
    <div class="content-wrapper">
        <div class="row">
            <SfGrid DataSource="EmployeeList" AllowPaging="true" Toolbar="toolbar">
                <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings>
                <GridColumns>
                    <GridColumn Field="@nameof(EmployeeDetails.OrderID)" HeaderText="Order ID" TextAlign="TextAlign.Right" IsPrimaryKey="true"> </GridColumn>
                    <GridColumn Field="@nameof(EmployeeDetails.CustomerName)" HeaderText="Customer Name" TextAlign="TextAlign.Left"> </GridColumn>
                    <GridColumn Field="@nameof(EmployeeDetails.EmployeeID)" HeaderText="Employee ID" TextAlign="TextAlign.Right"> </GridColumn>
                    <GridColumn Field="@nameof(EmployeeDetails.Freight)" HeaderText="Freight" TextAlign="TextAlign.Right" Format="C2"> </GridColumn>
                    <GridColumn Field="@nameof(EmployeeDetails.ShipCity)" HeaderText="Ship City" TextAlign="TextAlign.Left"> </GridColumn>
                    <GridColumn Field="@nameof(EmployeeDetails.ShipName)" HeaderText="Ship Name" TextAlign="TextAlign.Left"> </GridColumn>
                </GridColumns>

            </SfGrid>
        </div>
    </div>
</div>

@code
{
    List<EmployeeDetails> EmployeeList;
    string[] toolbar = new string[] { "Add", "Edit", "Delete", "Update", "Cancel" };
    protected override void OnInitialized()
    {
        base.OnInitialized();
        EmployeeList = Enumerable.Range(1, 20).Select(x => new EmployeeDetails()
        {
            OrderID = 10240 + x,
            CustomerName = new string[] { "VINET", "TOSMP", "HANAR", "VICTE" }[new Random().Next(4)],
            EmployeeID = x,
            Freight = new float[] { 32.28f, 22.90f, 30.99f, 50.52f }[new Random().Next(4)],
            ShipCity = new string[] { "Reims", "Munster", "Rio de Janeir", "Lyon" }[new Random().Next(4)],
            ShipName = new string[] { "Vins et alocools chevalie", "Toms Spezialitaten", "Hanari Carnes", "Supremes delices" }[new Random().Next(4)]
        }).ToList();
    }

    public class EmployeeDetails
    {
        public int OrderID { get; set; }

        public string CustomerName { get; set; }

        [CustomValidationEmployeeID]
        public int EmployeeID { get; set; }

        [CustomValidationFreight]
        public float Freight { get; set; }

        public string ShipCity { get; set; }

        public string ShipName { get; set; }
    }

    public class CustomValidationEmployeeID : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {

            if (value != null)
            {
                int employeeID = Convert.ToInt16(value);

                if (employeeID >= 1)
                {
                    return ValidationResult.Success;
                }
                else
                {
                    return new ValidationResult("EmployeeID value should be greater than zero");
                }
            }
            else
            {
                return new ValidationResult("EmployeeID value is required");
            }

        }
    }


    public class CustomValidationFreight : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                float freight = (float)value;

                if (freight >= 1 && freight <= 10000)
                {
                    return ValidationResult.Success;
                }
                else
                {
                    return new ValidationResult("Freight value should between 1 and 10,000");
                }
            }
            else
            {
                return new ValidationResult("Freight value  is required");
            }
        }

    }

}